import java.util.Arrays;

public class TestDemo {
    public static void main(String[] args) {
        int[] tmp = {1,3,5,7,9,11,13,15,17};
        int[] tmp2 = tmp.clone();//产生一个副本
        System.out.println(Arrays.toString(tmp));
        System.out.println(Arrays.toString(tmp2));
    }
    public static void main16(String[] args) {
        int[] array = {1,3,5,7,9,11,13,15,17};
        int[] array2 = Arrays.copyOfRange(array,2,5);
        //从下标为2开始拷贝，拷到下标为5的前一位
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(array2));
    }
    /*arraycopy
    */
    public static void main15(String[] args) {
        int[] array = {1,3,5,7,9};
        int[] array2 = new int[array.length];
        System.arraycopy(array,0,array2,0,array.length);
        int[] array3 = new int[array.length];
        //支持局部的拷贝：拷贝到数组的下标为3的位置，拷贝2个长度。
        System.arraycopy(array,0,array3,3,array.length-3);
        int[] array4 = new int[array.length];
        //支持局部的拷贝：从原数组的下标为2的位置开始拷，拷贝3个长度
        System.arraycopy(array,2,array4,0,array.length-2);

        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(array2));
        System.out.println(Arrays.toString(array3));
        System.out.println(Arrays.toString(array4));
    }
    public static void main14(String[] args) {
        int[] array = {1,3,5,7,9};
        //拷贝
        int[] array2 = Arrays.copyOf(array,array.length);
        //扩容2倍
        int[] array3 = Arrays.copyOf(array,array.length*2);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(array2));
        System.out.println(Arrays.toString(array3));
    }
    public static void main13(String[] args) {
        int[] array = {1,3,5,7,9};
        int[] array2 = new int[array.length];//Java中 new int[],[]中允许写变量
        for (int i = 0; i < array.length; i++) {
            array2[i] = array[i];
        }
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(array2));
    }
    /*自己实现一个toString - myToString
     * 将数组转变为字符串形式，然后输出
    */
    public static String myToString(int[] array){
        if(array == null){//先判断array是不是为null！！！
            return "null";
        }
        String ret = "[";
        for (int i = 0; i < array.length; i++) {
            ret += array[i];
            if(i< array.length-1){
                ret += ",";
            }
        }
       ret += "]";
        return ret;
    }
    public static void main12(String[] args) {
        int[] array =null;
        String ret = myToString(array);
        System.out.println(ret);
        int[] array2 ={1,2,3,4};
        String ret2 = myToString(array2);
        System.out.println(ret2);
    }

    public static void printArray(int[] array){
        for (int x:array) {
            System.out.print(x+" ");
        }
    }
    public static void main11(String[] args) {
        int[] array = {1,2,3,4,5,6,7,8,9,10};
        printArray(array);
    }
    //交换
    public static void swap(int[] tmp){
        int r = tmp[0];
        tmp[0] = tmp[1];
        tmp[1] = r;
    }
    public static void main10(String[] args) {
        int[] array = {1,2};
        System.out.println("交换前："+array[0]+" "+array[1]);
        swap(array);
        System.out.println("交换后："+array[0]+" "+array[1]);
    }
    public static int[] func(){
        int[] tmp = {1,2,3,4};
        return tmp;
    }
    public static void main9(String[] args) {
        int[] ret = func();
        System.out.println(Arrays.toString(ret));

    }
    public static void func1(int[] array1){
        array1 = new int[10];
    }
    public static void func2(int[] array2){
        array2[0] = 99;
    }
    public static void main8(String[] args) {
        int[] array = {1,2,3,4};
        func1(array);
        System.out.println(Arrays.toString(array));
        func2(array);
        System.out.println(Arrays.toString(array));
    }
    public static void main7(String[] args) {
        int[] array = null;
        System.out.println(array);
    }
    public static void main6(String[] args) {
        int[] array = {1,2,3,4};
        int[] array2 = {4,5,6,7};
        array = array2;
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(array2));
    }
    public static void main5(String[] args) {
        int[] array = {1,2,3,4};
        System.out.println(Arrays.toString(array));
        int[] array2 = array;
        array2[1] = 99;
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(array2));
    }
    public static void main4(String[] args) {
        int a = 10;
        int b = 20;
        int[] array = {1,2,5,7,9};//array是一个引用，类型是int[]

    }

    public static void main3(String[] args) {
        //遍历数组
        int[] array = {1,2,3,4};
        //（！！！）方法一：for循环
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }
        System.out.println();

        //（！！！）方法二：foreach也叫作增强for循环 (数组中每个元素的类型定义的一个变量:数组名)
        //可以这样理解：遍历array,每拿到一个元素，就存到x中，然后再将x打印出来
        for (int x:array) {
            System.out.print(x+" ");
        }
        System.out.println();
        //方法一和方法二的区别：foreach是拿不到数组下标的，for循环可以拿到数组下标

        //（！！！）方法三：Arrays.toString(数组名)
       /* Java当中有一个工具，可以专门用来操作数组，这个工具叫做Arrays。
        (用它需要导入包：import java.util.Arrays;)*/
        /*toString:返回指定数组的内容的字符串形式
        *         把数组转变为字符串形式，然后返回*/
       String ret = Arrays.toString(array);
       System.out.println(ret);

    }
    public static void main2(String[] args) {
        int[] array = {1,2,3,4};
        //修改和输出某个元素
        array[2] = 0;
        System.out.println(array[2]);
        //输出数组的长度：数组名.length
        System.out.println(array.length);
    }
    public static void main1(String[] args) {

        //第一种:只分配了内存，没初始化
        //没初始化，数组中存的是默认值
        int[] array3 = new int[5];

        int[] array5;
        array5 = new int[5];
        //第二种，初始化了
        //一步就初始化的代码，new int[]可省略
        int[] array1 = {1,2,3,4};

        int[] array2 = new int[]{1,2,3,4};
        //两步才初始化，new int[]必须有
        int[] array4;
        array4 = new int[]{1,2,3,4};


    }

}
